package com.hs.jhzd.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.hs.jhzd.entity.VideoPlay;
import com.hs.jhzd.entity.YlFyjhxxbEntity;
import com.hs.jhzd.repository.YlFyjhxxbRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

@Controller
@RequestMapping("/ylFyjhxxb")
@Slf4j
public class YlFyjhxxbController {

    @Autowired
    private YlFyjhxxbRepository repository;

    @Value("${video.path}")
    private String path;


    @RequestMapping("/findByRybh")
    @ResponseBody
    public List<YlFyjhxxbEntity> findByJsbh(String rybh) {
        Date endTime = new Date();
        Calendar c = Calendar.getInstance();
        //过去七天
        c.setTime(endTime);
        c.add(Calendar.DATE, -7);
        Date startTime = c.getTime();
        List<YlFyjhxxbEntity> byrybh = repository.findByrybh(rybh, startTime, endTime);
        return byrybh;
    }


    @RequestMapping("/save")
    @ResponseBody
    public boolean save(MultipartFile video, YlFyjhxxbEntity ylFyjhxxbEntity) {

        if (!video.isEmpty()) {

            String vpath = getVpath();
            File folder = new File(this.path + vpath);
            if (!folder.exists() && !folder.isDirectory()) {
                folder.mkdirs();
            }
            vpath += Calendar.getInstance().getTime().getTime() + "." + video.getOriginalFilename();
            BufferedOutputStream stream = null;
            try {
                stream = new BufferedOutputStream(new FileOutputStream(new File(this.path + vpath)));
                storeVideo(stream, video);
                Optional<YlFyjhxxbEntity> byId = repository.findByUuid(ylFyjhxxbEntity.getUuid().replaceAll("\"", ""));
                if(byId.isPresent()){
                    YlFyjhxxbEntity ylFyjhxxbEntity1 = byId.get();
                    ylFyjhxxbEntity1.setUrl(vpath);
                    repository.save(ylFyjhxxbEntity1);
                }
                return true;
            } catch (FileNotFoundException e) {
                log.error(e.getMessage());
            } finally {
                if (stream != null) {
                    try {
                        stream.close();
                    } catch (IOException e) {
                        log.error("文件流关闭异常");
                    }
                }
            }
        }
        return false;
    }

    private void storeVideo(BufferedOutputStream stream, MultipartFile video) {
        try {
            stream.write(video.getBytes());
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    log.error(e.getMessage());
                }
            }
        }
    }

    private String getVpath() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        return "/video/" + year + "/" + month + "/";
    }


}
